// 降维
import lodash from 'lodash'

const DimensionReduction = (array, key = null) => {
  if (!array || array.length === 0) { return [] }

  let ret = []
  if (key) {
    lodash.cloneDeep(array).forEach(item => {
      ret = ret.concat([item])

      if (!(!item[key] || item[key].length === 0)) {
        ret = ret.concat(DimensionReduction(item[key], key))
      }
    })
  } else {
    ret = lodash.flattenDeep(lodash.cloneDeep(array))
  }

  return ret
}

export default DimensionReduction
